package com.ruoyi.operation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.operation.domain.TBusisystem;
import com.ruoyi.operation.domain.TGentaskList;
import com.ruoyi.operation.domain.bo.TGentaskListBo;
import com.ruoyi.operation.domain.vo.TGentaskListVo;
import com.ruoyi.operation.domain.vo.TGentaskTreeVo;
import com.ruoyi.operation.mapper.TBusisystemMapper;
import com.ruoyi.operation.mapper.TGentaskListMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.operation.domain.bo.TGentaskBo;
import com.ruoyi.operation.domain.vo.TGentaskVo;
import com.ruoyi.operation.domain.TGentask;
import com.ruoyi.operation.mapper.TGentaskMapper;
import com.ruoyi.operation.service.ITGentaskService;

import java.util.*;

/**
 * 生成任务Service业务层处理
 *
 * @author chq
 * @date 2025-06-13
 */
@RequiredArgsConstructor
@Service
public class TGentaskServiceImpl implements ITGentaskService {

    private final TGentaskMapper baseMapper;

    private final TGentaskListMapper gentaskListMapper;

    private final TBusisystemMapper busisystemMapper;

    /**
     * 查询生成任务
     */
    @Override
    public TGentaskVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询生成任务列表
     */
    @Override
    public TableDataInfo<TGentaskVo> queryPageList(TGentaskBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TGentask> lqw = buildQueryWrapper(bo);
        Page<TGentaskVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询生成任务列表
     */
    @Override
    public List<TGentaskVo> queryList(TGentaskBo bo) {
        LambdaQueryWrapper<TGentask> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TGentask> buildQueryWrapper(TGentaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TGentask> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptid() != null, TGentask::getDeptid, bo.getDeptid());
        lqw.eq(StringUtils.isNotBlank(bo.getBusisystemid()), TGentask::getBusisystemid, bo.getBusisystemid());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TGentask::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getStepno()), TGentask::getStepno, bo.getStepno());
        lqw.like(StringUtils.isNotBlank(bo.getName()), TGentask::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getMemo()), TGentask::getMemo, bo.getMemo());
        lqw.eq(StringUtils.isNotBlank(bo.getPeriod()), TGentask::getPeriod, bo.getPeriod());
        lqw.eq(bo.getStarttime() != null, TGentask::getStarttime, bo.getStarttime());
        lqw.eq(StringUtils.isNotBlank(bo.getFlag()), TGentask::getFlag, bo.getFlag());
        return lqw;
    }

    /**
     * 新增生成任务
     */
    @Override
    public Boolean insertByBo(TGentaskBo bo) {
        TGentask add = BeanUtil.toBean(bo, TGentask.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改生成任务
     */
    @Override
    public Boolean updateByBo(TGentaskBo bo) {
        TGentask update = BeanUtil.toBean(bo, TGentask.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TGentask entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除生成任务
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<TGentaskTreeVo> listTree(String dateEight) {
        QueryWrapper<TGentaskList> wrapper = new QueryWrapper<>();
        wrapper.eq("taskdate", dateEight).last("order by gentaskid+0 asc");
        //这是当天所有的总数
        List<TGentaskListVo> tGentaskListVos = gentaskListMapper.selectVoList(wrapper, TGentaskListVo.class);
        //首先要获得前端父节点数据，父节点id是系统id，name是系统名称
        Set<TBusisystem> tBusisystems = new HashSet<>();    //这里存放父节点的系统名称
        for (TGentaskListVo e : tGentaskListVos) {
            TGentask tGentask = baseMapper.selectById(e.getGentaskid());
            e.setTGentask(tGentask);
            TBusisystem tBusisystem = busisystemMapper.selectById(tGentask.getBusisystemid());
            tBusisystems.add(tBusisystem);
        }

        List<TGentaskTreeVo> tGentaskTreeVos = new ArrayList<>();
        for (TBusisystem t : tBusisystems) {    //对所有有父节点的业务系统轮询
            TGentaskTreeVo tGentaskTreeParentVo = new TGentaskTreeVo(); //父节点内容，是系统信息
            tGentaskTreeParentVo.setId(t.getId());  //父节点附上业务系统id
            tGentaskTreeParentVo.setName(t.getName());  //父节点附上业务系统名称
            //children节点
            List<TGentaskTreeVo> children = new ArrayList<>();
            for (TGentaskListVo e : tGentaskListVos) {
                if (e.getTGentask().getBusisystemid().equals(t.getId())) {
                    TGentaskTreeVo tGentaskTreeChildrenVo = new TGentaskTreeVo();
                    tGentaskTreeChildrenVo.setId(e.getId());
                    tGentaskTreeChildrenVo.setName(e.getTGentask().getName());
                    tGentaskTreeChildrenVo.setType(e.getTGentask().getType());
                    tGentaskTreeChildrenVo.setMemo(e.getTGentask().getMemo());
                    tGentaskTreeChildrenVo.setStatus(TGentaskTreeVo.TaskstatusDict.getTaskstatusLabel(e.getTaskstatus()));
                    children.add(tGentaskTreeChildrenVo);
                }
            }
            tGentaskTreeParentVo.setChildren(children);
            tGentaskTreeVos.add(tGentaskTreeParentVo);
        }
        return tGentaskTreeVos;
    }
}
